using System;
using System.IO;
using System.Reflection;

namespace Umbraco.Ecommerce.BusinessLogic
{
    /// <summary>
    /// Summary description for PaymentFactory.
    /// </summary>
    public class PaymentMethod : Interfaces.IPaymentMethod
    {
        private static bool _isInitialized = false;
        private static string _pluginFolder = "";
        private static System.Collections.Hashtable _paymentMethods = new System.Collections.Hashtable();
        private Interfaces.IPaymentMethod _currentPayment;

        public PaymentMethod()
        {

        }

        public PaymentMethod(Guid IPaymentMethodd)
        {
            if (!_isInitialized)
                init();
            _currentPayment = Activator.CreateInstance(_paymentMethods[IPaymentMethodd].GetType()) as Interfaces.IPaymentMethod;
        }

        internal static string PlugInFolder
        {
            get
            {
                if (_pluginFolder == "")
                    return umbraco.GlobalSettings.Path + "/../bin";
                return _pluginFolder;
            }
            set
            {
                _pluginFolder = value;
            }
        }

        public static BusinessLogic.PaymentMethod[] GetAll()
        {
            if (!_isInitialized)
                init();
            BusinessLogic.PaymentMethod[] retVal = new BusinessLogic.PaymentMethod[_paymentMethods.Count];
            int c = 0;
            foreach (Interfaces.IPaymentMethod pm in _paymentMethods.Values)
            {
                retVal[c] = new PaymentMethod(pm.Id);
                c++;
            }
            return retVal;
        }

        private static void init()
        {
            System.Web.HttpContext.Current.Trace.Write("IPaymentMethod.factory", "Adding payment controls from directory: " + System.Web.HttpContext.Current.Server.MapPath(PlugInFolder));
            foreach (string assembly in System.IO.Directory.GetFiles(System.Web.HttpContext.Current.Server.MapPath(PlugInFolder), "*.dll"))
            {
                if (assembly.IndexOf("TidyDll.dll") == -1)
                {
                    System.Web.HttpContext.Current.Trace.Write("IPaymentMethod.factory", "Reading assembly " + assembly);
                    Assembly asm = System.Reflection.Assembly.LoadFrom(assembly);
                    try
                    {

                        foreach (Type t in asm.GetTypes())
                        {
                            try
                            {
                                if (t.FullName != new PaymentMethod().GetType().FullName)
                                {
                                    Type hasInterface = t.GetInterface("Umbraco.Ecommerce.Interfaces.IPaymentMethod", true);
                                    if (hasInterface != null && !t.IsInterface)
                                    {
                                        Interfaces.IPaymentMethod typeInstance = Activator.CreateInstance(t) as Interfaces.IPaymentMethod;
                                        _paymentMethods.Add(typeInstance.Id, typeInstance);
                                        System.Web.HttpContext.Current.Trace.Write("IPaymentMethod.factory", " + Adding editing plugin '" + typeInstance.Name);
                                    }
                                    else
                                        System.Web.HttpContext.Current.Trace.Write("IPaymentMethod.factory", " - Type '" + t.Name + " is not a plugin");
                                }
                                else
                                {
                                    System.Web.HttpContext.Current.Trace.Write("IPaymentMethod.factory", " - Type '" + t.Name + " is the factory class - now move on!");
                                }
                            }
                            catch { }
                        }
                    }
                    catch (Exception factoryE)
                    {
                        System.Web.HttpContext.Current.Trace.Warn("IPaymentMethod.Factory", "error in assembly: " + assembly, factoryE);
                    }
                }
            }
            _isInitialized = true;
        }
        #region IPaymentMethod Members

        public string Name
        {
            get
            {
                return _currentPayment.Name;
            }
        }

        public Guid Id
        {
            get
            {
                return _currentPayment.Id;
            }
        }

        public System.Web.UI.UserControl CollectCash
        {
            get
            {
                return _currentPayment.CollectCash;
            }
        }

        public bool CashCollected(int OrderId)
        {
            return _currentPayment.CashCollected(OrderId);
        }

        #endregion
    }
}
